package solution;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-01-18
 * Time: 13:53
 */
public class Solution1 {
    public static void main(String[] args) {
        int[] arr = {1, 2, 3, 4, 2, 1};
        System.out.println(Arrays.toString(findNumberAppearOnce(arr)));

    }

    public static int[] findNumberAppearOnce(int[] arr) {
        int num = 0;
        for (int j : arr) {
            num ^= j;
        }
        int[] ints = new int[2];
        int num1 = 0;
        int num2 = 0;
        int index = binaryIndex(num);
        for (int j : arr) {
            if (((j >> index) & 1) == 1) {
                num1 ^= j;
            } else {
                num2 ^= j;
            }
        }
        ints[0] = num1;
        ints[1] = num2;
        return ints;
    }

    public static int binaryIndex(int num) {
        int index = 0;
        while ((num & 1) == 0 && index < 32) {
            num >>= 1;
            index++;
        }
        return index;
    }
}
